package com.qdlc.p2p.dal.model;

import java.util.List;

import org.springframework.beans.BeanUtils;

import com.qdlc.p2p.common.exception.BusinessException;
import com.qdlc.p2p.common.exception.ScoreException;
import com.qdlc.p2p.common.util.BeanUtil;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.dao.GoodsDao;
import com.qdlc.p2p.dal.dto.Goods;
import com.qdlc.p2p.dal.dto.GoodsCategory;
import com.qdlc.p2p.dal.dto.GoodsPic;

public class GoodsModel extends Goods {

	/** 0未审核 */
	public static final byte WAIT_AUDIT = 0;
	/** 1审核通过 */
	public static final byte PASS_AUDIT = 1;
	/** -1审核不通过 */
	public static final byte NOT_PASS_AUDIT = -1;

	/** 0待上架 */
	public static final byte WAIT_SHELVES = 0;
	/** 1上架 */
	public static final byte UP_SHELVES = 1;
	/** -1下架 */
	public static final byte DOWN_SHELVES = -1;

	/** 商品图片 */
	private List<GoodsPic> picList;

	/** 后台图片上传 */
	private List<String> files;

	/** 第一张图片 */
	private String imgSrc;

	/** 开始时间 **/
	private String startTime;

	/** 结束时间 **/
	private String endTime;

	/** 商品类别名称 **/
	private String categoryName;

	/** 积分范围：开始积分 **/
	private int startScore;

	/** 积分范围：结束积分 **/
	private int endScore;

	/** 操作者ID */
	private long operatorId;

	/** 商品类别 */
	private long categoryId;

	/** 操作备注 */
	private String operatorRemark;

	/** 当前页数 **/
	private int page;

	/** 分页数 **/
	private int rows;

	/** 条件查询 **/
	private String searchName;
	
	/** 是否需要审核 */
	private boolean needVerify;

	public static GoodsModel instance(Goods item) {
		GoodsModel model = new GoodsModel();
		BeanUtils.copyProperties(item, model);
		return model;
	}

	public Goods prototype() {
		Goods item = new Goods();
		BeanUtils.copyProperties(this, item);
		return item;
	}
	
	/**
	 * 校验数据
	 */
	public void checkModel() {
		if (StringUtil.isBlank(getDescription())) {
			throw new ScoreException("商品详情不能为空", BusinessException.TYPE_JSON);
		}
		if (StringUtil.isBlank(getAttribute())) {
			throw new ScoreException("商品属性不能为空", BusinessException.TYPE_JSON);
		}
	}

	public Goods prototypeForModify() {
		GoodsDao goodsDao = BeanUtil.getBean(GoodsDao.class);
		Goods goods = goodsDao.find(getId());
		goods.setGoodsCategory(new GoodsCategory(getCategoryId()));
		goods.setName(getName());
		goods.setScore(getScore());
		goods.setMarketPrice(getMarketPrice());
		goods.setStore(getStore());
		goods.setIsVirtual(getIsVirtual());
		goods.setDescription(getDescription());
		goods.setAttribute(getAttribute());
		goods.setRemark(getRemark());

		goods.setUpdateTime(DateUtil.getNow());
		;
		goods.setUpdateOperator(getUpdateOperator());
		return goods;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public String getImgSrc() {
		return imgSrc;
	}

	public void setImgSrc(String imgSrc) {
		this.imgSrc = imgSrc;
	}

	public List<GoodsPic> getPicList() {
		return picList;
	}

	public void setPicList(List<GoodsPic> picList) {
		this.picList = picList;
	}

	public int getStartScore() {
		return startScore;
	}

	public void setStartScore(int startScore) {
		this.startScore = startScore;
	}

	public int getEndScore() {
		return endScore;
	}

	public void setEndScore(int endScore) {
		this.endScore = endScore;
	}

	public List<String> getFiles() {
		return files;
	}

	public void setFiles(List<String> files) {
		this.files = files;
	}

	public long getOperatorId() {
		return operatorId;
	}

	public void setOperatorId(long operatorId) {
		this.operatorId = operatorId;
	}

	public String getOperatorRemark() {
		return operatorRemark;
	}

	public void setOperatorRemark(String operatorRemark) {
		this.operatorRemark = operatorRemark;
	}

	public long getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(long categoryId) {
		this.categoryId = categoryId;
	}

	public String getSearchName() {
		return searchName;
	}

	public void setSearchName(String searchName) {
		this.searchName = searchName;
	}

	public String getCategoryName() {
		return categoryName;
	}

	public void setCategoryName(String categoryName) {
		this.categoryName = categoryName;
	}

	public boolean isNeedVerify() {
		return needVerify;
	}

	public void setNeedVerify(boolean needVerify) {
		this.needVerify = needVerify;
	}

}
